package myauctioneer.dao;

import java.sql.*;
import java.util.ArrayList;

import myauctioneer.App;
import myauctioneer.dto.User;

public class UserDAO implements UserDAOI {

	private PreparedStatement saveStmt;
	private PreparedStatement loadStmt1;
	private PreparedStatement loadStmt2;
	private PreparedStatement loadStmtAll;
	private PreparedStatement removeStmt;

	public UserDAO() {
		try {
			saveStmt = App.getDb().prepareStatement(
					"INSERT INTO customer(name, address, phone, email, password) "
							+ "VALUES (?, ?, ?, ?, ?)");
			loadStmt1 = App.getDb().prepareStatement(
					"SELECT * FROM customer " + "WHERE email = ?");
			loadStmt2 = App.getDb().prepareStatement(
					"SELECT * FROM customer " + "WHERE customer_id = ?");

			loadStmtAll = App.getDb().prepareStatement(
					"SELECT * FROM customer");

			removeStmt = App.getDb().prepareStatement(
					"DELETE FROM customer WHERE customer_id = ?");
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void saveUser(User u) {
		try {
			saveStmt.setString(1, u.getName());
			saveStmt.setString(2, u.getAddress());
			saveStmt.setString(3, u.getPhone());
			saveStmt.setString(4, u.getEmail());
			saveStmt.setString(5, u.getPassword());
			saveStmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected User load(ResultSet result) throws SQLException
	{
		User u = new User();

		u.setUserId(result.getInt("customer_id"));
		u.setName(result.getString("name"));
		u.setAddress(result.getString("address"));
		u.setPhone(result.getString("phone"));
		u.setEmail(result.getString("email"));
		u.setPassword(result.getString("password"));

		return u;
	}

	@Override
	public User loadUser(String email) {
		User u = null;

		try {
			loadStmt1.setString(1, email);
			ResultSet result = loadStmt1.executeQuery();

			if (result.next())
				u = this.load(result);
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return u;
	}
	
	@Override
	public User loadUserByUserId(int userId) {
		User u = null;

		try {
			loadStmt2.setInt(1, userId);
			ResultSet result = loadStmt2.executeQuery();

			if (result.next())
				u = this.load(result);
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return u;
	}

	@Override
	public ArrayList<User> loadAllUsers() {
		ArrayList<User> users = new ArrayList<User>();

		try {
			ResultSet result = loadStmtAll.executeQuery();

			while (result.next())
				users.add(this.load(result));
		}
		catch (SQLException e) {
			App.getLogger().info(e.getMessage());
			e.printStackTrace();
		}

		return users;
	}

	@Override
	public boolean remove(int id)
	{
		try {
			removeStmt.setInt(1, id);
			return removeStmt.executeUpdate() > 0;
		}
		catch (SQLException e) {
			e.printStackTrace();
		}

		return false;
	}
}
